import 'package:flutter/material.dart';
import 'package:ywn_app/contains/ThemeGlobal.dart';

class Demo02 extends StatefulWidget {
  @override
  _Demo02State createState() => _Demo02State();
}

class _Demo02State extends State<Demo02> with SingleTickerProviderStateMixin{

  TabController tabController;

  @override
  void initState() {
    super.initState();
    this.tabController = TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          body: CustomScrollView(
            physics: NeverScrollableScrollPhysics(),
            slivers: <Widget>[
              SliverToBoxAdapter(
                child: Text('ded'),
              ),
              SliverPersistentHeader(
                pinned: false,
                delegate: StickyTabBarDelegate(
                    child: TabBar(
                      indicatorColor: themeColor,
                      indicatorSize: TabBarIndicatorSize.label,
                      labelColor: themeColor,
                      controller: tabController,
                      unselectedLabelColor: Colors.black54,
                      tabs: <Widget>[
                        Tab(text: '详情'),
                        Tab(text: '评论(54)'),
                      ],
                    )),
              ),
              SliverFillRemaining(
                child: TabBarView(
                    controller: this.tabController,
                    children: [
                      Text("01"),
                      Text('02')
                    ]
                ),
              )
            ],
          ),
        )
    );
  }
}

class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar child;
  StickyTabBarDelegate({@required this.child});
  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return this.child;
  }
  @override
  double get maxExtent => this.child.preferredSize.height - 15;
  @override
  double get minExtent => this.child.preferredSize.height - 15;
  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
